package cc.rengu.igas.momp.common.dao.impl;

import cc.rengu.igas.momp.common.dao.McTaskFlowMapper;
import cc.rengu.igas.momp.common.entity.TMMcCouponRuleInfo;
import cc.rengu.igas.momp.common.entity.TMMcOrgInfo;
import cc.rengu.igas.momp.common.entity.TMMcTaskFlow;
import cc.rengu.jradp.mods.dbs.Database;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.utility.util.DateUtil;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 营销活动列表查询数据库操作接口实现
 *
 * @author liujinan
 * @version 1.0.0
 * @date 2021/4/6 13:51
 */
public class McTaskFlowMapperImpl implements McTaskFlowMapper {

    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppParamConstant.DATABASE_CONF_NODE, AppConfigConstant.MOMP_POOL_NAME);

    @Override
    public List<TMMcTaskFlow> selectMcTaskFlowList(String taskSts) throws Exception {

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectMcTaskFlowList");

        // 按接口赋查询条件
        TMMcTaskFlow flow = new TMMcTaskFlow();
        flow.setTaskSts(taskSts);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, flow, TMMcTaskFlow.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("获取营销活动任务流水列表失败!当前无未处理的任务流水");
            return null;
        }

        return objectList.stream().map(item -> (TMMcTaskFlow) (item)).collect(Collectors.toList());
    }

    @Override
    public TMMcTaskFlow selectMcTaskFlowByFlowNo(String instId, String mcNo, String flowNo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectMfdMcDetailInfoByBankAuditRole");

        TMMcTaskFlow taskFlow = new TMMcTaskFlow();
        taskFlow.setMcNo(mcNo);
        taskFlow.setInstId(instId);
        taskFlow.setFlowNo(flowNo);

        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, taskFlow, taskFlow);
        if (Database.DBS_SUCCESS != iReturnCode) {
            rglog.error("根据条件查询活动任务表，法人机构号<{}>营销活动编号<{}>流水号<{}>获取营销活动任务流水失败!<{}>", instId, mcNo, flowNo, iReturnCode);
            return null;
        }

        return taskFlow;
    }

    @Override
    public int insertMcTaskFlow(TMMcTaskFlow tMMcTaskInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "insertMcTaskFlow");

        int iReturnCode = dbsUtil.dbsExecuteTransactionSql(sqlStr, tMMcTaskInfo, TMMcTaskFlow.class);
        if (Database.DBS_SUCCESS != iReturnCode) {
            rglog.error("插入法人机构号<{}>营销活动编号<{}>流水号<{}>的定时任务信息失败!<{}>", tMMcTaskInfo.getInstId(), tMMcTaskInfo.getMcNo(), tMMcTaskInfo.getFlowNo(), iReturnCode);
            return iReturnCode;
        }

        return Database.DBS_SUCCESS;
    }

    @Override
    public int updateMcTaskFlowStt(String instId, String mcNo, String flowNo, String taskStt) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateMcTaskFlowStt");

        TMMcTaskFlow tMMcTaskFlow = new TMMcTaskFlow();
        tMMcTaskFlow.setInstId(instId);
        tMMcTaskFlow.setMcNo(mcNo);
        tMMcTaskFlow.setFlowNo(flowNo);
        tMMcTaskFlow.setTaskSts(taskStt);
        tMMcTaskFlow.setModifyTm(DateUtil.getCurrentDateTime("yyyy-MM-dd HH:mm:ss.SSS"));
        int iReturnCode = dbsUtil.dbsExecuteTransactionSql(sqlStr, tMMcTaskFlow, TMMcTaskFlow.class);
        if (Database.DBS_SUCCESS != iReturnCode && Database.DBS_NOT_FOUND != iReturnCode) {
            rglog.error("更新法人机构号<{}>营销活动编号<{}>的任务流水<{}>任务状态<{}>失败!<{}>", instId, mcNo, flowNo, taskStt, iReturnCode);
            return iReturnCode;
        }

        return Database.DBS_SUCCESS;
    }

    @Override
    public int updateMcTaskFlowBizDate(String instId, String mcNo, String flowNo, String bizDate) throws Exception {

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateMcTaskFlowBizDate");

        TMMcTaskFlow tMMcTaskFlow = new TMMcTaskFlow();
        tMMcTaskFlow.setFlowNo(flowNo);
        tMMcTaskFlow.setInstId(instId);
        tMMcTaskFlow.setMcNo(mcNo);
        tMMcTaskFlow.setBizTypeParamData(bizDate);
        int iReturnCode = dbsUtil.dbsExecuteTransactionSql(sqlStr, tMMcTaskFlow, TMMcTaskFlow.class);

        if (Database.DBS_SUCCESS != iReturnCode && Database.DBS_NOT_FOUND != iReturnCode) {
            rglog.error("更新法人机构号<{}>营销活动编号<{}>的任务流水表业务数据<{}>失败!<{}>", instId, mcNo, bizDate, iReturnCode);
            return iReturnCode;
        }

        return Database.DBS_SUCCESS;
    }
}
